/**
 * Copyright 2014 the original author or authors. All rights reserved.
 */
package com.visionet.security.filter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.visionet.jumper.core.common.Message;
import com.visionet.security.base.enums.SecurityMessage;
import com.visionet.security.utils.WebUtils;
import org.apache.shiro.session.SessionException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 *
 * @author suxiaojing
 * @date 
 * @since 0.0.1
 */
public class RestLogoutFilter extends LogoutFilter {
	
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	/* (non-Javadoc)
	 * @see org.apache.shiro.web.filter.authc.LogoutFilter#preHandle(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
	 */
	@Override
	protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
		Subject subject = getSubject(request, response);
        //try/catch added for SHIRO-298:
        try {
            subject.logout();
        } catch (SessionException ise) {
        	logger.debug("Encountered session exception during logout.  This can generally safely be ignored.", ise);
        }
		Message loginMsg = new Message(SecurityMessage.SUCCESS.getCode(), "logout safely");
		WebUtils.writeJsonResponse(loginMsg, WebUtils.toHttp(response));
        return false;
	}
	
}
